﻿package com.oneko.reproductores {
	import flash.display.SimpleButton;
	import flash.media.Video;
	import flash.media.SoundTransform;
	import flash.net.NetStream;
	import flash.net.NetConnection;
	import com.oneko.utils.Debugger;
	import com.oneko.controls.PlayerButtonPlay;
	import flash.events.NetStatusEvent;
	import flash.events.IOErrorEvent;
	import flash.events.AsyncErrorEvent;
	import flash.events.ErrorEvent;
	import flash.events.HTTPStatusEvent;
	import flash.events.MouseEvent;
	import flash.display.Sprite;
	import com.greensock.TweenMax;
	
	
	public class VideoPlayer extends SimpleButton{
		
		// Constants:
		// Public Properties:
		public function set url(u:String):void{
			_url=u;
			stream.close();
			drawDisplay(0,0);
		}
		public function get url():String{
			return _url;
		}
		// Private Properties:
		
		private var _url:String;
		private var conexion:NetConnection;
		private var stream:NetStream;
		public var video:Video;
		private var sound:SoundTransform;
		private var wid:Number;
		private var hei:Number;
		public var display:Sprite;
		
		// Initialization:
		public function VideoPlayer(w:Number,h:Number) { 
			wid=w;
			hei=h;
			display=new Sprite();
			initPlayer();
//			display.addChild(new PlayerButtonPlay(w,h));
			addEventListener(MouseEvent.CLICK,playVideo);
		}
		
		private function drawDisplay(w,h):void{
			display.graphics.clear();
			display.graphics.beginFill(0x000000,1);
			display.graphics.drawRoundRect(0,0,w,h,30);
			display.graphics.endFill();
		}
		
		private function initPlayer():void{
			conexion = new NetConnection();
			conexion.connect(null);
			
			stream = new NetStream(conexion);
			stream.checkPolicyFile = true;
			stream.addEventListener(NetStatusEvent.NET_STATUS, streamHandler); 
			stream.addEventListener(IOErrorEvent.IO_ERROR,errorHandler);
			stream.addEventListener(AsyncErrorEvent.ASYNC_ERROR,errorHandler);
			stream.bufferTime = 2;
			
			video = new Video();
			video.height = 391;
			video.width = 551;
			video.x = 407.6;
			video.y = 187.4;
			
			var obj:Object = new Object();
			stream.client = obj;
			obj.onMetaData = this.MetaData;
			video.attachNetStream(stream);

			sound = new SoundTransform();
			stream.soundTransform = sound;
		}
		
		private function MetaData(info:Object) {
			Debugger.log(info);
		}
		
		public final function setVisible(mostrar:Boolean):void{
			this.visible = mostrar;
			this.video.visible = mostrar ;
			if (mostrar) {
				this.stream.resume();
			} else {
				this.stream.pause();
			}
		}
		
		private function playVideo(evt:MouseEvent):void{
			drawDisplay(1,1);
			display.x=x;
			display.y=y;
			//parent.addChild(display);
			parent.addChild(video);
			
			video.mask=display;
			TweenMax.to(display,0.5,{width:551,height:391,x:407.6,y:187.4,onUpdate:redrawDisplay});
			
			Debugger.log("Playing!");
			stream.play(url);
		}
		
		private function redrawDisplay():void{
			drawDisplay(display.width,display.height);
		}
	
		private function errorHandler(evt:ErrorEvent):void {
			Debugger.log("Error generico al cargar el video;",evt.text);
		};

		private function streamHandler(evt:NetStatusEvent){
			Debugger.log("Info del video",evt.info.code);
			if (evt.info.code=="NetStream.Play.Stop"){
				stream.close();
				display.graphics.clear();
				this.video.visible = false;
			}
		}

		public function onMouseClick(func:Function):void {
			addEventListener(MouseEvent.CLICK,func);
		}
		
		public function onStreamHandler(func:Function):void {
			stream.addEventListener(NetStatusEvent.NET_STATUS, func); 
		}		
		
		private function maskAndPlay():void{
		}
	}
	
}